define(['jquery','api','user','text!pages/user/customer.html','Vue'],function($,api,user){
	var vm, 
	    startid,
	    count=10,
	    username,
	   controller = {
		   init:function(){
		   	   if(!user.checkLogin())return;
			   api.initFooter(3);
			   api.setTitle('客户信息')
			   if(!vm){
			     this.render();
			   }else{
			     vm.show=true;
			     this.doQuery();
			   }
		   },
		   render:function(){
		   	   var that=this;
		   	   $('#pages').append('<div id="customer"></div>');
		       require(['text!pages/user/customer.html','Vue'],function(tpl,Vue){
		            vm=new Vue({
		               el:'#customer',
		               data:{
		               	  show:true,
		               	  showLoading:true,
		               	  username:'',
		               	  showMore:false,
		               	  rows:false
		               },
		               methods:{
		                  doQuery:function(){
			              	that.doQuery();
			              },
			              loadMore:function(){
			                that.load();
			              }
		               },
		               template:tpl
		            });
		            that.doQuery();
		       })
		   },
		   doQuery:function(){
		   	  username=vm.username;
		   	  vm.rows=[];
		   	  vm.showLoading=true;
		   	  startid=0;
		   	  this.load();
		   },
		   load:function(){
		   	  var rows,
		   	      len;
		   	  api.customer({username:username,startuid:startid,count:count,mobile:''},function(rs){
		   	  	   vm.showLoading=false;
		   	       if(rs.retcode=='0'){
		   	       	  if(rs.agentuser){
		   	       	  	rows=rs.agentuser;
		   	       	  	len=rows.length;
		   	          	vm.rows=vm.rows.length?vm.rows.concat(rows):rows;
		   	            startid=rows[len-1].uid;
		   	            vm.showMore=len<count?false:true;
		   	          }else{
		   	            vm.showMore=false;
		   	          }
		   	       }
		   	  });
		   },
		   onRouteChange:function(){
		       if(vm)vm.show=false;
		   }
		}
    return controller;
})